package com.clps.cn.Activite03;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

/**
 * 单个流程的挂起
 */
public class SuspendProcessDefinitionSingle {

    public static void main(String[] args) {

        //01得到ProcessEngine对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        //02得到runTimeService
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        //03：查询到流程实例的对象
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("2501").singleResult();
        //得到流程中的实例是否为暂停状态
        boolean suspended = processInstance.isSuspended();
        String processInstanceId = processInstance.getId();
        //判断
        if(suspended){
            //说明暂停，可以激活操作
            runtimeService.activateProcessInstanceById(processInstanceId);
            System.out.println("流程"+processInstanceId+"激活");
        }else{
            runtimeService.suspendProcessInstanceById(processInstanceId);
            System.out.println("流程定义"+processInstanceId+"挂起");
        }

    }


    /**
     * 测试挂起状态下。流程是否被执行，挂起不能成功，不能成功，是否怎么所，抛异常。。。。。。。。。
     */
    @Test
    public  void  test01(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = defaultProcessEngine.getTaskService();

        //03：根据流程定义的key得到任务表
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("holiday")
                .taskAssignee("zhangsan")
                .list();



        //任务列表
        for (Task task : list) {
            System.out.println("task.getProcessInstanceId() = " + task.getProcessInstanceId());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
            System.out.println("task.getName() = " + task.getName());

            taskService.complete(task.getId());

        }
    }
}
